﻿using Lung_Sounds_V2;
using LungSoundWizard.View;
using Microsoft.Win32;
using pHMb.Spectrogram;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Input;

namespace LungSoundWizard.ViewModel
{
    public class OpenExistingViewModel : WizardViewModelBase
    {
        public override string Title
        {
            get { return "Lung sound editor"; }
        }

        RelayCommand _openAvgFftCommand;
        public ICommand OpenAvgFftCommand
        {
            get
            {
                if (_openAvgFftCommand == null)
                {
                    _openAvgFftCommand = new RelayCommand(p => OpenAvgFft(((IList)p).Cast<SpectrogramPane.XSelection>().ToList()), 
                        p => ((IList)p).Count > 0);
                }
                return _openAvgFftCommand;
            }
        }

        RelayCommand _openWaveCommand;
        public ICommand OpenWaveCommand
        {
            get
            {
                if (_openWaveCommand == null)
                {
                    _openWaveCommand = new RelayCommand(p => OpenFile());
                }
                return _openWaveCommand;
            }
        }

        string _filePath;
        public string FilePath
        {
            get { return _filePath; }
            set { SetProperty(ref _filePath, value); }
        }


        // This shouldn't be done!
        readonly LiveSpectrogramView _spectView;
        public LiveSpectrogramView SpectView
        {
            get { return _spectView; }
        }

        bool _isProvidedPath = false;
        public bool IsProvidedPath
        {
            get { return _isProvidedPath; }
            set { SetProperty(ref _isProvidedPath, value); }
        }

        public OpenExistingViewModel()
        {
            _spectView = new LiveSpectrogramView();

            _spectView.DataContext = this;
            _spectView.IsRunning = false;
            _spectView.IsRecording = false;
            _spectView.SetBinding(LiveSpectrogramView.FilePathProperty,  new Binding("FilePath"));
            _spectView.Name = "spectView";
        }

        public override void OnShow()
        {
            if (FilePath == null)
            {
                OpenFile();
            }
        }

        public void OpenFile()
        {
            OpenFileDialog openDialog = new OpenFileDialog();

            openDialog.Filter = "Wave File (*.wav)|*.wav";

            if (openDialog.ShowDialog() == true)
            {
                FilePath = openDialog.FileName;
            }
        }

        void OpenAvgFft(List<SpectrogramPane.XSelection> selections)
        {
            if (selections.Count > 0)
            {
                Dictionary<string, double[]> avgFfts = SoundSelectionHandler.GenerateAverageFFT(selections);
                
                new GraphWindow(avgFfts, "Average FFT (Compared to 'normal')", "Frequency (Hz)", "Deviation from Vesicular", (1.00 / selections[0].ParentPane.WindowSize)).Show();
            }
        }
    }
}
